<?php

namespace Common\Model;

use Think\Model;

/**
 * Description of HotelApplyModel
 * 
 * @author songanwei
 * @date 2015-12-31
 */
class HotelApplyModel extends BaseModel {

    protected $trueTableName = 'tb_hotel_apply';

    protected function _before_insert(&$data, $options) {
        // parent::_before_insert($data, $options);
    }

    protected function _before_update(&$data, $options) {
        //parent::_before_update($data, $options);
    }

    /**
     * 手机号返回客栈申请数据(未处理、已处理条件结果)
     * @modify songanwei 2015-12-31
     * @param string $mobile  手机号
     * @param string $areaCode  手机区号
     * @return type
     */
    public function getInfoByMobile($mobile, $areaCode = '86') {
        $where = array(
            'mobile' => $mobile,
            'areacode' => $areaCode,
            'status' => array('in', array(0, 1))
        );
        return $this->where($where)->find();
    }

    /**
     * 手机号返回客栈申请数据(未处理、已处理条件结果)
     * @modify songanwei 2015-12-31
     * @param string $mobile  手机号
     * @param string $areaCode  手机区号
     * @return type
     */
    public function getInfoByMobileAndAreaCode($mobile, $areaCode, $status = null) {
        $where = array(
            'mobile' => $mobile,
            'areacode' => $areaCode,
        );
        if (!is_null($status)) {
            $where['status'] = array('in', $status);
        }
        return $this->where($where)->select();
    }

    /**
     * 经销商返回客栈申请数据(未处理、已处理条件结果)
     * @modify zhangzhen 2017-1-5
     * @param string $mobile  手机号
     * @return type
     */
    public function getInfoByDealerId($dealerId) {
        $where = array(
            'dealerlogin_id' => $dealerId,
            'status' => array('in', array(0, 2))
        );
        return $this->where($where)->select();
    }

    /**
     * 添加申请数据
     * @modify songanwei 20145-12-31
     * @modify zhangzhen 2017-1-5 
     * @modify zhangzhen 2017-05-11 添加时区 区号
     * @param string $hotelName 客栈名称
     * @param string $cityName 城市名称
     * @param string $mobile 手机号
     * @param string $userName 用户
     * @param string $applyfrom  申请来源
     * @param int $universalTimeId  时区 Id
     * @param int $areaCode  区号
     * @param int $cityId  城市id
     * @param int $districtId  地区id
     * @param int $communityNo  集群号
     * @param int $masterentityId  集群id
     * @param int $enddate  结束时间
     * @param  integer   $currencyId 货币id
     * @return type
     */
    public function addInfo($hotelName, $cityName, $mobile, $userName, $dealerId, $from, $universalTimeId = '5', $areaCode = '86', $countryId = null, $cityId = null, $districtId = null, $communityNo = null, $masterentityId = null, $enddate = null,  $currencyId = null) {
        $options = array(
            'hotelname' => $hotelName,
            'cityname' => $cityName,
            'mobile' => $mobile,
            'username' => $userName,
            'dealerlogin_id' => $dealerId,
            'from' => $from,
            'universaltime_id' => $universalTimeId,
            'areacode' => $areaCode,
        );
        if (!is_null($countryId)) {
            $options['country_id'] = $countryId;
        }
        if (!is_null($cityId)) {
            $options['city_id'] = $cityId;
        }
        if (!is_null($districtId)) {
            $options['district_id'] = $districtId;
        }
        if (!is_null($communityNo)) {
            $options['communityno'] = $communityNo;
        }
        if (!is_null($masterentityId)) {
            $options['community_id'] = $masterentityId;
        }
        if (!is_null($enddate)) {
            $options['enddate'] = $enddate;
        }
        if (!is_null($currencyId)) {
            $options['currency_id'] = $currencyId;
        }

        return $this->add($options);
    }
    /**
     * 修改申请数据
     * @modify zhangzhen 2017-1-6 
     * @param string $hotelName 客栈名称
     * @param string $cityName 城市名称
     * @param string $mobile 手机号
     * @param string $userName 用户
     * @param string $applyfrom  申请来源
     * @return type
     */
    public function saveInfo($applyId, $hotelName, $cityName, $mobile, $userName) {
        $options = array(
            'hotelname' => $hotelName,
            'cityname' => $cityName,
            'mobile' => $mobile,
            'username' => $userName,
            'status' => 0,
        );
        $where = array('id' => $applyId);
        return $this->where($where)->save($options);
    }

}
